package zw_801_900.zw_832_翻转图像;

class Solution {
    public static void main(String[] args) {
        int[][] image = {{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}};
        int[][] invertImage = flipAndInvertImage(image);
        for (int[] ints : invertImage) {
            for (int i : ints) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    /**
     * 一次遍历 + 双指针
     *
     * @param image
     * @return
     */
    public static int[][] flipAndInvertImage(int[][] image) {
        int n = image.length;
        for (int i = 0; i < n; i++) {
            int left = 0, right = n - 1;
            while (left < right) {
                if (image[i][left] == image[i][right]) {
                    image[i][left] ^= 1;
                    image[i][right] ^= 1;
                }
                left++;
                right--;
            }
            if (left == right) {
                image[i][left] ^= 1;
            }
        }
        return image;
    }


    /**
     * 两次遍历
     * @param image
     * @return
     */
//    public static int[][] flipAndInvertImage(int[][] image) {
//        for (int i = 0; i < image.length; i++) {
//            for (int j = 0; 2 * j < image[0].length; j++) {
//                int temp = 1 - image[i][j];
//                image[i][j] = 1 - image[i][image[0].length - j - 1];
//                image[i][image[0].length - j - 1] = temp;
//            }
//        }
//        return image;
//    }
}
